package com.coolbi.security.web.action;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.security.biz.RoleToUserBiz;
import com.coolbi.security.biz.RoleUrlBiz;
import com.coolbi.security.entity.RoleUrl;
import com.coolbi.security.entity.Roles;
import com.coolbi.security.entity.User;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class RoleUrlAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private List<RoleUrl> roleUrlList;
	private RoleUrl roleUrl;
	private User user;

	@Autowired
	private RoleUrlBiz roleUrlBiz;
	public void setRoleUrlBiz(RoleUrlBiz roleUrlBiz) {
		this.roleUrlBiz = roleUrlBiz;
	}
	
	@Autowired
	private RoleToUserBiz roleToUserBiz;
	public void setRoleToUserBiz(RoleToUserBiz roleToUserBiz) {
		this.roleToUserBiz = roleToUserBiz;
	}

	/** 添加URL资源 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "roleUrl.name", message = "URL名称必填"),
			@RequiredStringValidator(fieldName = "roleUrl.url", message = "URL地址必填")})
	public String addRoleUrl() throws Exception {
		roleUrlBiz.save(roleUrl);
		return SUCCESS;
	}

	/**根据用户ID获取所有URL信息*/
	public String list() {
		user = getCurrentUser();
		List<Roles> roles = roleToUserBiz.findRoleByUserId(user.getId());
		List<Roles> GroupRoles = roleToUserBiz.findGroupRoleByUserId(user.getId());
		roles.addAll(GroupRoles);
		roleUrlList = roleUrlBiz.findUrlByRoles(roles);
		Collections.sort(roleUrlList);
		return SUCCESS;
	}
	
	/**
	 * 获取所有URL信息
	 * @return
	 * @throws Exception
	 */
	public String listAll() {
		roleUrlList = roleUrlBiz.list();
		Collections.sort(roleUrlList);
		return SUCCESS;
	}
	
	/**获取所有URL资源信息并分页*/
	public String listByPage() throws Exception {
		pager.setRecordCount(roleUrlBiz.count());
		roleUrlList = roleUrlBiz.list((pager.getCurrPageNo() - 1) * pager.getPageSize(),  pager.getPageSize());
		return SUCCESS;
	}
	
	/** 删除 */
	public String delete() throws Exception {
		roleUrlBiz.delete(roleUrl.getId());
		roleUrlList = roleUrlBiz.list();
		return SUCCESS;
	}

	/** 根据ID查询URL信息 */
	public String findUrlById() throws Exception {
		roleUrl = roleUrlBiz.get(roleUrl.getId());
		roleUrlList = roleUrlBiz.list();
		return SUCCESS;
	}
	
	/**修改URL资源排序顺序*/
	public String updateOrderNo() throws Exception {
		roleUrlBiz.updateOrderNo(roleUrl);
		return NONE;
	}
	
	/**在链接树上修改节点名称*/
	public String updateNameInTree() throws Exception {
		roleUrlBiz.updateNameInTree(roleUrl);
		return NONE;
	}

	/** 修改 */
	public String update() throws Exception {
		roleUrlBiz.update(roleUrl);
		return SUCCESS;
	}

	/** 获取所有URL信息 */
	public String findAllUrl() throws Exception {
		roleUrl = roleUrlBiz.get(roleUrl.getParentId());
		roleUrlList = roleUrlBiz.list();
		return SUCCESS;
	}
	
	/**检测链接名称是否已存在*/
	public String checkUrlName() {
		try {
			boolean flag = false;
			if (null == roleUrl.getId()) {
				flag = roleUrlBiz.countByUrlName(roleUrl.getName());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**检测链接地址是否已存在*/
	public String checkUrl() {
		try {
			boolean flag = false;
			if (null == roleUrl.getId()) {
				flag = roleUrlBiz.countByUrl(roleUrl.getUrl());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}

	public List<RoleUrl> getRoleUrlList() {
		return roleUrlList;
	}

	public void setRoleUrlList(List<RoleUrl> roleUrlList) {
		this.roleUrlList = roleUrlList;
	}

	public RoleUrl getRoleUrl() {
		return roleUrl;
	}

	public void setRoleUrl(RoleUrl roleUrl) {
		this.roleUrl = roleUrl;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
